package com.dlc.shop.user.platform.task;

import cn.hutool.core.collection.CollUtil;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.dlc.shop.user.common.service.CostPerPopupService;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 弹窗广告定时任务
 * @author TRACK
 */
@Component
@AllArgsConstructor
public class PopupTask {

    private static final Logger log = LoggerFactory.getLogger(PopupTask.class);

    private final CostPerPopupService costPerPopupService;

    /**
     * 改变弹窗广告状态
     * 半小时执行一次
     */
    @XxlJob("changePopup")
    public void changePopup() {
        log.info("改变弹窗广告状态");
        List<Long> shopIds = costPerPopupService.listShouldChangePopUp();
        if (CollUtil.isEmpty(shopIds)) {
            return;
        }
        // 需要投放的弹窗广告
        List<Long> putOnIds = costPerPopupService.putOnAdvertise();
        // 需要结束的弹窗广告
        List<Long> endIds = costPerPopupService.endAdvertise();
        putOnIds.addAll(endIds);
        if (CollUtil.isNotEmpty(putOnIds)) {
            costPerPopupService.removeCacheBatch(putOnIds, shopIds);
        }
    }
}
